package com.yulongtian.algorithms.doublepointer.syntropy;

import java.util.Arrays;

/**
 * 同向双指针
 * 力扣题目209
 * https://leetcode.cn/problems/minimum-size-subarray-sum/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-09 10:58
 */
public class Test01 {
    public static void main(String[] args) {

    }

    public static int minSubArrayLen(int target, int[] nums) {
        //数组长度
        int len = nums.length;
        //定义到最大
        int ans = len + 1;

        //左指针
        int left = 0;
        //双指针之间数的和
        int sum = 0;
        //右指针往后移动
        for (int right = 0; right < len; right++) {
            //加上右指针的数
            sum += nums[right];
            //如果以右指针为端点的左边数满足条件，则右移左指针
            while (sum >= target) {
                ans = Math.min(ans, right - left + 1);
                sum -= nums[left];
                left++;
            }

        }
        //判断，可能没有满足条件
        return ans <= len ? ans : 0;
    }

}
